package base.sys

import android.app.Activity
import android.app.AlarmManager
import android.app.Application
import android.app.PendingIntent
import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import base.sys.LauncherApplication.static.UrlType
import base.sys.LauncherApplication.static.isDevelop
import base.utils.DeviceIdUtil
import base.utils.LogUtil
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.ToastUtils
import base.utils.SharedPreferencesUtils
import com.bumptech.glide.request.target.ViewTarget
import java.lang.ref.WeakReference
import java.util.*


/**
 * Created by Administrator on 2018/4/27.
 */
open class LauncherApplication : Application() {

    private val activities = Stack<WeakReference<Activity>>()
    var mContext: Context? = null
    private var activityVisible: Boolean = false
    private var sharedPreferences: SharedPreferences? = null

    object static {
        //用object修饰，相当于Java中的static，用object修饰一个变量，可以实现全局变量的效果
        var customerProductSearchUrl: String? = ""
        var randomTv: String? = ""
        var randomPic: String = ""
        var random: String? = ""
        var addresshtml: String? = null
        var tempAddresshtml: String? = null
        var UrlType = UAT
        var isDevelop = true
    }

    override fun onCreate() {
        super.onCreate()
//        Bugtags.start("58e8df83c16e05da4db2be42983089c3", this, Bugtags.BTGInvocationEventNone)
        ApplicationContext.application = Application()
        ApplicationContext.launcherApplication = this
        ApplicationContext.mContext = applicationContext


        BASE_URL = REAL

        UrlType = getEnvironmentType()
        BASE_URL = getEnviromentURL()
        init()
    }

    private fun getEnviromentURL(): String {
        return if (isDevelop) {
            if (SharedPreferencesUtils.getInt(
                    SP_ENVIRONMENT_INFO,
                    ENVIRONMENT_TYPE
                ) == 0
            ) {
                REAL
            } else {
                val type = SharedPreferencesUtils.getInt(SP_ENVIRONMENT_INFO, ENVIRONMENT_TYPE)
                if (type == DEV) {
                    DEMO
                } else if (type == SIT) {
                    DEMO
                } else {
                    REAL
                }
            }
        } else {
            REAL
        }
    }

    private fun getEnvironmentType(): Int {
        return if (isDevelop) {
            if (SharedPreferencesUtils.getInt(
                    SP_ENVIRONMENT_INFO,
                    ENVIRONMENT_TYPE
                ) == 0
            ) {
                UAT
            } else {
                SharedPreferencesUtils.getInt(
                    SP_ENVIRONMENT_INFO,
                    ENVIRONMENT_TYPE
                )
            }
        } else {
            UAT
        }
    }


    companion object {

        fun setEnvironmentType(type: Int) {
            SharedPreferencesUtils.putInt(
                SP_ENVIRONMENT_INFO,
                ENVIRONMENT_TYPE,
                type
            )
        }

//        fun showSelectEnvironmentPopUp(mContext: Context) {
//            val chooseEnvironmentPopUp = ChooseEnvironmentPopUp(mContext)
//            chooseEnvironmentPopUp.setPopupGravity(Gravity.BOTTOM)
//            chooseEnvironmentPopUp.showPopupWindow()
//            when (UrlType) {
//                DEV -> chooseEnvironmentPopUp.rbDev.setChecked(true)
//                SIT -> chooseEnvironmentPopUp.rbSit.setChecked(true)
//                UAT -> chooseEnvironmentPopUp.rbUat.setChecked(true)
//            }
//            chooseEnvironmentPopUp.tvSure.setOnClickListener(View.OnClickListener {
//                if (chooseEnvironmentPopUp.rbDev.isChecked()) {
//                    setEnvironmentType(DEV)
//                }else if (chooseEnvironmentPopUp.rbSit.isChecked()) {
//                    setEnvironmentType(SIT)
//                } else if (chooseEnvironmentPopUp.rbUat.isChecked()) {
//                    setEnvironmentType(UAT)
//                }
//                setToken("")
//                setShopId("")
//                ToastUtils.showShort("已成功切换环境，请重新打开app")
//                chooseEnvironmentPopUp.dismiss()
//                val intent = mContext.packageManager
//                    .getLaunchIntentForPackage((mContext as Activity).packageName)
//                val restartIntent =
//                    PendingIntent.getActivity(mContext.getApplicationContext(), 0, intent, 0)
//                val mgr = mContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
//                mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, restartIntent) // 1秒钟后重启应用
//                AppUtils.exitApp()
//            })
//        }
    }



    private fun init() {
        sharedPreferences = getSharedPreferences(SP_SHOPTV_INFO, Context.MODE_PRIVATE)
        ApplicationContext.application = this
    }

    @Synchronized
    fun putActivity(activity: Activity) {
        val rf = WeakReference(activity)
        activities.push(rf)
    }

    @Synchronized
    fun popActivity() {
        val rf = activities.pop()
        val activity = rf.get()
        if (null != activity) {
            activity.finish()
        }
    }

    fun peekActivity(): Activity? {
        return if (!activities.isEmpty()) {
            activities.peek().get()
        } else {
            null
        }
    }

    @Synchronized
    fun finishAllActivity() {
        while (!activities.empty()) {
            val rf = activities.pop()
            val activity = rf.get()
            activity?.finish()
        }
    }

    fun getContext(): Context {
        return ApplicationContext.mContext!!
    }

    fun isActivityVisible(): Boolean {
        return activityVisible
    }

    fun activityResumed() {
        activityVisible = true
    }

    fun activityPaused() {
        activityVisible = false
    }


}